<template>
  <el-dialog
    v-model="dialogVisibled"
    title="新增"
    width="500px"
    :before-close="() => handleClose(ruleFormRef)"
    :align-center="true"
  >
    <el-form :model="form" ref="ruleFormRef">
      <el-form-item label="用户名" prop="user" :label-width="formLabelWidth">
        <el-input
          v-model="form.user"
          autocomplete="off"
          placeholder="请输入"
          style="width: 320px"
        />
      </el-form-item>
      <el-form-item label="分数" prop="score" :label-width="formLabelWidth">
        <el-select
          v-model="form.score"
          placeholder="请选择"
          style="width: 320px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="详情" prop="title" :label-width="formLabelWidth">
        <el-input
          v-model="form.title"
          autocomplete="off"
          placeholder="请输入"
          style="width: 320px"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="onSubmit(ruleFormRef)">确定</el-button>
      <el-button @click="onCancel(ruleFormRef)"> 取消 </el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive, computed, ref, toRefs } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance } from "element-plus";
export default defineComponent({
  name: "AddList",
  props: ["isDialog", "closeDialog", "options", "addList", "formDtata"],
  setup(props, context) {
    const ruleFormRef = ref<FormInstance>();
    const formLabelWidth = "100px";
    let form = ref(
      computed(() => {
        return props.formDtata;
      })
    );
    /* 关闭事件 */
    const handleClose = (formEl?: FormInstance | undefined) => {
      ElMessageBox.confirm("是否确实要关闭此对话框", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          props.closeDialog();
          if (!formEl) return;
          formEl.resetFields();
          // ElMessage({
          //   type: "success",
          //   message: "添加成功",
          // });
        })
        .catch(() => {
          // ElMessage({
          //   type: "info",
          //   message: "Delete canceled",
          // });
        });
    };
    /* 取消按钮 */
    const onCancel = (formEl?: FormInstance | undefined) => {
      props.closeDialog();
      if (!formEl) return;
      formEl.resetFields();
    };
    /* 确定按钮 */
    const onSubmit = (formEl?: FormInstance | undefined) => {
      props.closeDialog();
      props.addList(form.value);
      if (!formEl) return;
      formEl.resetFields();
    };
    /* 计算属性 */
    // const dialogVisibled = computed(() => {
    //   return props.isDialog;
    // });

    const dialogVisibled = computed({
      get: () => props.isDialog,
      set: () => {
        console.error();
      },
    });

    return {
      ruleFormRef,
      form,
      formLabelWidth,
      handleClose,
      dialogVisibled,
      onSubmit,
      onCancel,
    };
  },
});
</script>

<style lang="scss" scoped>
</style>